package com.agency8x.controller;

import java.util.List;

import com.agency8x.config.AppConf;
import com.agency8x.interceptor.CheckLoginInterceptor;
import com.agency8x.model.AdminModel;
import com.agency8x.model.LoginModel;
import com.agency8x.web.util.ControllerUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

/**
 * 用户公用页面控制器
 * 
 * @author SoChishun
 *
 */
@Before(CheckLoginInterceptor.class)
public class UserController extends Controller {
	public void profile() {
		this.redirect("/admin/profile");
	}

	public void password_edit() {
		LoginModel loginBean = LoginModel.getLoginInfo(this, AppConf.loginSessionID);
		if (null == loginBean) {
			this.renderText("登录超时或未登录");
			return;
		}
		AdminModel model = AdminModel.me.findById(loginBean.getUserId());
		if (null == model) {
			this.renderText("记录不存在");
			return;
		}
		this.setAttr("user_id", model.getUserId());
		render("password_edit.html");
	}

	public void doSavePassword() {
		Integer user_id = this.getParaToInt("user_id");
		String password = this.getPara("password");
		String repassword = this.getPara("repassword");
		if (null == user_id || null == password || null == repassword) {
			ControllerUtil.renderDisptchJumpError(this, "表单数据无效");
		}
		if (!password.equals(repassword)) {
			ControllerUtil.renderDisptchJumpError(this, "两次密码输入不一致");
		}
		boolean result = AdminModel.me.updatePassword(user_id, password);
		if (result) {
			ControllerUtil.renderDisptchJumpSuccess(this); // 操作成功，倒计时3秒后自动刷新列表页面
		} else {
			ControllerUtil.renderDisptchJumpError(this, "保存失败"); // 操作失败，倒计时3秒后，自动后退到提交的表单页面
		}

	}

	public void role() {
		String user_id=this.getPara();
		List<AdminModel> list=AdminModel.me.findAdmins(user_id,"user_id, user_name, role_id");
		this.setAttr("userList", list);
		this.render("role.html");
	}

	public void doSaveRole() {
		String user_id=this.getPara("user_id[]");
		String role_id=this.getPara("role_id[]");
		boolean result=AdminModel.me.saveRole(user_id, role_id);
		if (result) {
			ControllerUtil.renderDisptchJumpSuccess(this); // 操作成功，倒计时3秒后自动刷新列表页面
		} else {
			ControllerUtil.renderDisptchJumpError(this, "保存失败"); // 操作失败，倒计时3秒后，自动后退到提交的表单页面
		}
	}

	public void department() {
		this.render("department.html");
	}

	public void save_department() {
		this.renderText("save!!!");
	}
	public void changeStatus(){
		Integer user_id=this.getParaToInt("user_id");
		Integer status=this.getParaToInt("status");
		boolean result=AdminModel.me.changeStatus(user_id, status);
		this.renderJson("{\"status\":true}");
	}
}
